<template>
  <div class="setting-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 左侧 -->
          <el-tab-pane label="角色管理">
            <!-- 按钮 -->
            <el-button
              icon="el-icon-plus"
              size="small"
              type="primary"
              @click="showDialog = true"
            >新增角色</el-button>
            <!-- 表格 -->
            <el-table v-loading="loading" :data="roleList">
              <el-table-column
                label="序号"
                width="100"
                type="index"
                :index="indexMethod"
              />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{ row }">
                  <el-button
                    size="small"
                    type="success"
                    @click="opendialog(row.id)"
                  >分配权限</el-button>
                  <el-button
                    size="small"
                    type="primary"
                    @click="putapi(row)"
                  >编辑</el-button>
                  <el-button
                    size="small"
                    type="danger"
                    @click="delapi(row)"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!--分页 -->
            <div class="box">
              <el-pagination
                :current-page="form.page"
                :page-sizes="[10, 20, 30, 40]"
                :page-size="form.pagesize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </div>
            <!-- 弹窗 -->
            <el-dialog
              :title="title"
              :visible="showDialog"
              @close="closeDialog"
            >
              <el-form
                ref="roleForm"
                :model="list"
                :rules="rules"
                label-width="100px"
              >
                <el-form-item label="角色名称" prop="name">
                  <el-input v-model="list.name" placeholder="请输入角色名称" />
                </el-form-item>
                <el-form-item label="角色描述" prop="description">
                  <el-input
                    v-model="list.description"
                    placeholder="请输入角色描述"
                  />
                </el-form-item>
              </el-form>

              <template #footer>
                <el-button @click="closeDialog">取消</el-button>
                <el-button type="primary" @click="addApi">确认</el-button>
              </template>
            </el-dialog>
          </el-tab-pane>

          <el-tab-pane label="公司信息">
            <!-- 警告信息 -->
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <!-- 表单 -->
            <!-- 表单 -->
            <el-form label-width="120px" style="margin-top: 50px">
              <el-form-item label="公司名称">
                <el-input
                  v-model="companyForm.name"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input
                  v-model="companyForm.companyAddress"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input
                  v-model="companyForm.mailbox"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  v-model="companyForm.remarks"
                  type="textarea"
                  :rows="3"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
    <!-- 分配权限的弹窗 -->
    <el-dialog

      class="assign-role"
      title="分配角色"
      :visible="showRoleDialog"
      :close-on-click-modal="false"
      @close="shutdialog"
      @open="dialogOpenFn"
    >
      <!-- show-checkbox 表示节点是否可以被选择 -->
      <!-- check-strictly 父子节点不关联 -->
      <!-- default-expand-all 是否默认展开 -->
      <el-tree
        ref="tree"
        v-loading="loadings"
        :data="permissionList"
        :props="{ label: 'name' }"
        show-checkbox
        check-strictly
        default-expand-all
        node-key="id"
      />

      <template #footer>
        <el-button type="primary" size="small" @click="assignPerm">确定</el-button>
        <el-button size="small" @click="shutdialog">取消</el-button>
      </template>
    </el-dialog>
  </div>
</template>
<script>
import {
  getsettingApi,
  delsettingApi,
  addsettingApi,
  xiusettingApi,
  putsettingApi,
  getgongsi,
  assignPermApi
} from '@/api/setting'
import { getpermissin } from '@/api/permission'
import { getTree } from '@/utils/index'

export default {
  name: 'Setting',
  data() {
    return {
      // 获取角色列表的两个参数
      form: {
        page: 1,
        pagesize: 10
      },
      // 存放弹窗的绑定
      list: {
        name: '',
        description: ''
      },
      // 弹窗的规则
      rules: {
        name: [{ required: true, message: '请输入角色名称', trigger: 'blur' }],
        description: [
          { required: true, message: '请输入角色描述', trigger: 'blur' }
        ]
      },
      // 存放角色列表的数据
      roleList: [],
      // 存放共有多少条数据
      total: 0,
      // loading的效果
      loading: false,
      // 弹窗的效果，添加角色
      showDialog: false,
      // 存放当前公司的信息
      companyForm: {},
      showRoleDialog: false, // 分配的弹窗
      roleId: '',
      permissionList: [], // 分配权限分配的数据
      loadings: false // 分配权限的loading
    }
  },
  // 计算属性判断弹窗标题
  computed: {
    title() {
      return this.list.id ? '编辑角色' : '添加角色'
    }
  },
  created() {
    this.getsetting()
    this.getgongsi()
  },
  methods: {
  // 点击权限弹窗确定时候
    async assignPerm() {
      // console.log(this.$refs.tree.getCheckedNodes())
      // console.log(this.$refs.tree.getCheckedKeys())
      const permIds = this.$refs.tree.getCheckedKeys()
      await assignPermApi({ id: this.roleId, permIds })
      this.$message.success('分配权限成功')
      this.shutdialog()
    },
    // 打开权限弹窗的回调
    async dialogOpenFn() {
      this.loadings = true
      await this.getPermissionList()
      await this.getPermissionDetailFn()
      this.loadings = false
    },
    /**
     * 回显已有权限
     */
    async getPermissionDetailFn() {
      const {
        data: { permIds }
      } = await xiusettingApi(this.roleId)
      // 回显
      this.$refs.tree.setCheckedKeys(permIds)
    },
    /**
     * 获取所有权限
     */
    async getPermissionList() {
      const { data } = await getpermissin()
      this.permissionList = getTree(data, '0')
    },
    // 打开分配权限的弹窗
    opendialog(id) {
      this.showRoleDialog = true
      this.roleId = id
    },
    // 关闭分配权限的弹窗
    shutdialog() {
      this.showRoleDialog = false
    },
    // 获取列表的数据
    async getsetting() {
      // 转圈圈
      this.loading = true
      const {
        data: { rows, total }
      } = await getsettingApi(this.form)
      // console.log(rows)
      // console.log(total)
      // 回填数据
      this.roleList = rows
      this.total = total
      // 数据成功转圈圈结束
      this.loading = false
    },
    // 改变当前多少条每页的回调
    async handleSizeChange(val) {
      this.form.pagesize = val
      this.form.page = 1
      this.getsetting()
    },
    // 改变当前页数的回调
    handleCurrentChange(val) {
      this.form.page = val
      this.getsetting()
    },
    // 做序号的函数
    indexMethod(index) {
      return (this.form.page - 1) * this.form.pagesize + index + 1
    },
    // 点击删除的方法
    delapi(row) {
      this.$confirm('你确认要进行删除么?', '温馨提示')
        .then(async() => {
          // 删除操作成功
          await delsettingApi(row.id)
          // console.log(res)
          if (this.roleList.length === 1 && this.form.page > 1) {
            this.form.page--
          }
          this.getsetting()
          // 添加提示
          this.$message.success('删除操作成功')
        })
        .catch((error) => {
          console.log(error)
        })
    },
    // 点击确定要全体校验并调用接口的方法
    addApi() {
      this.$refs.roleForm.validate(async(valid) => {
        if (!valid) return
        if (this.list.id) {
          const res = await putsettingApi(this.list)
          console.log(res)
          this.$message.success('恭喜修改成功')
        } else {
          await addsettingApi(this.list)
          this.$message.success('恭喜添加成功')
        }
        // 掉借口哦成功之后重新渲染页面，并关闭弹窗
        this.getsetting()
        this.closeDialog()
      })
    },
    // 让弹窗关闭
    closeDialog() {
      this.showDialog = false
      this.$refs.roleForm.resetFields()
      this.list = {
        name: '',
        description: ''
      }
    },
    // 点击编辑的时候
    async putapi(row) {
      const { data } = await xiusettingApi(row.id)
      // console.log(data)
      this.list = data
      this.showDialog = true
    },
    // 获区当前公司的信息
    async getgongsi() {
      const res = await getgongsi(this.$store.state.user.infor.companyId)
      // console.log(res)
      this.companyForm = res.data
    }
  }
}
</script>

<style>
.box {
  padding: 10px;
  display: flex;
  justify-content: flex-end;
}
</style>
